package it.davial.mgwt.server;

import static it.softphone.commons.util.LogUtil.logger; 
import static it.davial.mgwt.server.Utility.ManagmentId;
import static  it.davial.mgwt.server.Sql.Figure.*;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;


import it.davial.mgwt.client.Area;
import it.davial.mgwt.client.Figure;
import it.davial.mgwt.client.Photo;


public class FigureUtils {

	
	/**
	 * find all figure without criteria
	 * @return List<Patient>
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Figure> findAllFigure()throws Exception{
		
		EntityManager newEm=null;
		List<Figure> result=new ArrayList<Figure>();
		Utility.Logs.startFunction("FigureUtils", "findAllFigure", new String[]{});
		
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			
			result =(List<Figure>)newEm.createNativeQuery(SELECT_ALL,Figure.class).getResultList();
	
			logger.info(" Figure(s) found:".concat(String.valueOf(result.size()))  );
		
		}catch(javax.persistence.NoResultException e){
			logger.warn("FigureUtils:findAllFigure",e);
		}catch(Exception e){
			logger.error("FigureUtils:findAllFigure",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return result;
	}
	
	/**
	 * find all figure without criteria
	 * @return List<Figure>
	 * @throws Exception
	 */
	public static Figure findFigureById(int id)throws Exception{
		
		EntityManager newEm=null;
		Figure result=null;
		Utility.Logs.startFunction("FigureUtils", "findFigureById", new String[]{String.valueOf(id)});
		
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			
			
			result =(Figure)newEm.createNativeQuery(SELECT_BY_ID,Figure.class).
										setParameter(1,id ).getSingleResult();
	
			logger.info(" Figure found:".concat(String.valueOf(result))  );
            
			
		}catch(javax.persistence.NoResultException e){
			logger.warn("FigureUtils:findFigureById",e);
		}catch(Exception e){
			logger.error("FigureUtils:findFigureById",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return result;
	}
	

	/**
	 * find all photo without criteria
	 * @return List<Photo>
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Area> findAreaByIdFigure(int idFigure)throws Exception{
		
		EntityManager newEm=null;
		List<Area> result=null;
		Utility.Logs.startFunction("PhotoUtils", "findAreaById", new String[]{String.valueOf(idFigure)});
		
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			
			
			result =(List<Area>)newEm.createNativeQuery(SELECT_AREA,Area.class).
										setParameter(1,idFigure ).getResultList();
	
			logger.info(" Photo found:".concat(String.valueOf(result))  );
			

		}catch(javax.persistence.NoResultException e){
			logger.warn("PhotoUtils:findAreaById",e);
		}catch(Exception e){
			logger.error("PhotoUtils:findAreaById",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return result;
	}
	
	/**
	 * create a figure
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean createFigure(Figure figure)throws Exception{
		EntityManager newEm=null;
		EntityTransaction tx =null;
		Utility.Logs.startFunction("FigureUtils", "createFigure", new String[]{figure.toString()});
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			tx = newEm.getTransaction();
		    tx.begin();
		    figure.setIdFigure(ManagmentId.getNextId(IdEntity.FIGURE));
		    figure.setDataInsert(Utility.Date.nowDateDb());
		    figure.setDataUpdate(Utility.Date.nowDateDb());
			newEm.persist(figure);
			logger.info("figure created..." );
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			logger.error("FigureUtils:createFigure",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return true;
	}
	
	/**
	 * merge a figure
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean mergeFigure(Figure figure)throws Exception{
		EntityManager newEm=null;
		EntityTransaction tx =null;
		Utility.Logs.startFunction("FigureUtils", "merge", new String[]{figure.toString()});
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			tx = newEm.getTransaction();
		    tx.begin();
		    figure.setDataUpdate(Utility.Date.nowDateDb());
		    figure.setDataInsert(Utility.Date.webFormatToDateDb(figure.getDataInsert()));

			newEm.merge(figure);
			logger.info("figure mergeed..." );
			 tx.commit();
		}catch(Exception e){
			tx.rollback();
			logger.error("PatientUtils:mergePatient",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return true;
	}
	
	/**
	 * delete a figure
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean deleteFigure(Figure figure)throws Exception{
		EntityManager newEm=null;
		EntityTransaction tx =null;
		Utility.Logs.startFunction("FigureUtils", "delete", new String[]{figure.toString()});
		try{
			EntityManagerFactory emf=JpaUtility.getEntityFactory();
			if (emf==null){
				logger.warn("EntityManagerFactory is null...");
				throw new Exception();
			}
			newEm =emf.createEntityManager();
			tx = newEm.getTransaction();
		    tx.begin();
		    figure.setState(Utility.Costants.DISABLED);
			logger.info(String.format("figure delete...[%s]",figure) );
			figure.setDataUpdate(Utility.Date.nowDateDb());
		    figure.setDataInsert(Utility.Date.webFormatToDateDb(figure.getDataInsert()));

			newEm.merge(figure);
			
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			logger.error("FigureUtils:deleteFigure",e);
			throw e;
		}finally{
			if (newEm!=null)newEm.close();
		}
		return true;
	}
	
	
}
